/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.leadiv.cards;

import com.leadiv.cards.Rook.RookCard;
import com.leadiv.cards.Rook.RookPlayer;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;

/**
 *
 * @author pborrego
 */
public class Player implements RookPlayer {
    private Vector<RookCard> hand;
    private Vector<RookPlayer> partners;

    public Player(RookCard hand[]) {
        this.hand = new Vector<RookCard>(Arrays.asList(hand));
        this.partners = new Vector<RookPlayer>();
        this.orderHand();
    }

    public Player()
    {
        this.hand = new Vector<RookCard>();
        this.partners = new Vector<RookPlayer>();
    }

    public void addPartner(RookPlayer partner)
    {
        this.partners.add(partner);
    }

    public void clearPartners()
    {
        this.partners.clear();
    }

    public void showHand() {
        Iterator<RookCard> iter = hand.iterator();
        Card c;
        String output = "";

        while(iter.hasNext())
        {
            c = iter.next();

            output += c.getSuit() + " " + c.getFace() + ", ";
        }
        output += ";";
        output = output.replace(", ;", "");

        System.out.println(output);
    }

    private void orderHand() {
        Collections.sort((List<RookCard>)this.hand);
    }

    public String chooseTrump(Vector<RookCard> kitty)
    {
        int kittysize = kitty.size();
        int i = 0;
        int handSize, trumpIndex;

        this.hand.addAll(kitty);
        kitty.clear();
        for(; i<kittysize; i++)
        {
            handSize = (int) (Math.random() * this.hand.size());
            kitty.add(this.hand.remove(handSize));
        }

        this.orderHand();
        trumpIndex = (int) (Math.random() * this.hand.size());

        if(this.hand.get(trumpIndex).getSuit().equals("Trump")) {
            trumpIndex = (trumpIndex + 1) % this.hand.size();
        }

        return this.hand.get(trumpIndex).getSuit();
    }

    public int bid()
    {
        return (int) ((Math.random() * 11) + 14) * 5;
    }

    /**
     * play
     * Removes a card from the player's hand and puts it in the trick.
     * @param t
     */
    public void play(Trick t)
    {
        Vector<RookCard> cards = this.availableCards(t);
        RookCard card;

        // Randomly choose a card.
        card = cards.get((int) (Math.random() * cards.size()));

        t.play(card);

        // Remove card from our hand.
        this.hand.remove(card);
    }

    private Vector<RookCard> availableCards(final Trick t)
    {
        Vector<RookCard> available = new Vector<RookCard>();
        RookCard c;
        Iterator<RookCard> iter = this.hand.iterator();

        while(iter.hasNext())
        {
            c = iter.next();

            if(t.isEmptyTrick())
            {
                available.add(c);
                continue;
            }

            if(c.getSuit().equals("Trump") || c.getSuit().equals(t.getLedSuit()))
                available.add(c);
        }

        if(available.isEmpty())
            return this.hand;

        return available;
    }

    public int availableTricks()
    {
        return this.hand.size();
    }

    public void newHand(RookCard[] hand) {
        this.hand.clear();
        this.hand.addAll(Arrays.asList(hand));
    }
}
